package com.cdxy.util;

/**
 * @Author: tangy
 * @Date: 2020/4/7 18:24
 */
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

public class MoneyUtil {
    public MoneyUtil() {
    }

    public static Integer convertYuanToFen(String amount) {
        Integer amLong = 0;
        if (StringUtils.isEmpty(amount)) {
            amount = "";
        }

        int index = amount.indexOf(".");
        int length = amount.length();
        if (index == -1) {
            amLong = Integer.valueOf(amount + "00");
        } else if (length - index >= 3) {
            amLong = Integer.valueOf(amount.substring(0, index + 3).replace(".", ""));
        } else if (length - index == 2) {
            amLong = Integer.valueOf(amount.substring(0, index + 2).replace(".", "") + 0);
        } else {
            amLong = Integer.valueOf(amount.substring(0, index + 1).replace(".", "") + "00");
        }

        return amLong;
    }

    public static String convertFenToYuanDecimal(String fen) {
        String yuan = "0.00";
        StringBuffer result = new StringBuffer();
        String transFen = fen;
        boolean isMinus = fen.startsWith("-");
        if (isMinus) {
            transFen = fen.substring(1, fen.length());
        }

        boolean MULTIPLIER = true;
        Pattern pattern = Pattern.compile("^[1-9][0-9]*{1}");
        Matcher matcher = pattern.matcher(transFen);
        if (matcher.matches()) {
            yuan = (new BigDecimal(transFen)).divide(new BigDecimal(100)).setScale(2).toString();
            String intString = yuan.substring(0, yuan.length() - 3);

            for(int i = 1; i <= intString.length(); ++i) {
                if ((i - 1) % 3 == 0 && i != 1) {
                    result.append(",");
                }

                result.append(intString.substring(intString.length() - i, intString.length() - i + 1));
            }

            result.reverse().append(".").append(yuan.substring(yuan.length() - 2));
            yuan = result.toString();
            if (isMinus) {
                yuan = "-" + yuan;
            }
        } else {
            yuan = "0.00";
        }

        return yuan;
    }

    public static String convertFenToYuanDecimal(Integer fen) {
        return convertFenToYuanDecimal(fen.toString());
    }

    public static String convertFenToYuanNoDecimal(String fen) {
        return StringUtils.replace(convertFenToYuanDecimal(fen), ".00", "");
    }

    public static String convertToYuan(String fen) {
        String yuan = "0.00";
        new StringBuffer();
        String transFen = fen;
        boolean isMinus = fen.startsWith("-");
        if (isMinus) {
            transFen = fen.substring(1, fen.length());
        }

        boolean MULTIPLIER = true;
        Pattern pattern = Pattern.compile("^[1-9][0-9]*{1}");
        Matcher matcher = pattern.matcher(transFen);
        if (matcher.matches()) {
            yuan = (new BigDecimal(transFen)).divide(new BigDecimal(100)).setScale(2).toString();
            if (isMinus) {
                yuan = "-" + yuan;
            }
        } else {
            yuan = "0";
        }

        yuan = StringUtils.replace(yuan, ".00", "");
        return yuan;
    }

    public static Integer fenCeil(String amount) {
        String yuan = BigDecimal.valueOf((long)Integer.valueOf(amount)).divide(new BigDecimal(100)).toString();
        Double yuanDouble = Double.valueOf(yuan);
        yuanDouble = Math.ceil(yuanDouble);
        return convertYuanToFen(String.valueOf(yuanDouble));
    }

    public static BigDecimal toBigDecimalByString(String amount) {
        BigDecimal bd = new BigDecimal(amount);
        bd = bd.setScale(2, 4);
        return bd;
    }
}

